import { vpnBeatCheck } from 'admin/servers/servers';
import logoutSys from 'utils/modules/logoutSys';

const REACT_APP_LOGIN_VPN_CHECK = process.env.REACT_APP_LOGIN_VPN_CHECK == '1';

const time = 60 * 1000; // 60秒监听一次心跳

const vpnBeat = () => {
  console.log('vpn心跳监听');
  vpnBeatCheck().catch(err => {
    console.error('vpn心跳监听异常，退出系统', err);
    logoutSys(true);
  });
};

// vpn心跳监听
export const vpnCheckInit = () => {
  if (!REACT_APP_LOGIN_VPN_CHECK) return;
  if (window.vpnCheckTimer) return;
  console.log('vpn心跳监听开启');

  const timer = setInterval(
    (() => {
      vpnBeat();
      return vpnBeat;
    })(),
    time
  );
  window.vpnCheckTimer = timer;
};

// 清除vpn心跳监听
export const vpnCheckClear = () => {
  if (!REACT_APP_LOGIN_VPN_CHECK) return;
  if (window.vpnCheckTimer) {
    clearInterval(window.vpnCheckTimer);
  }
};
